//
//  MyOrderTopView.m
//  Findif
//
//  Created by Mac on 2018/11/1.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyOrderTopView.h"

@interface MyOrderTopView ()

@property (nonatomic, strong)UIButton * allButton;
@property (nonatomic, strong)UIButton * moneyButton;
@property (nonatomic, strong)UIButton * useButton;
@property (nonatomic, strong)UIButton * commitButton;
@property (nonatomic, strong)UIView * allLineView;
@property (nonatomic, strong)UIView * moneyLineView;
@property (nonatomic, strong)UIView * useLineView;
@property (nonatomic, strong)UIView * commitLineView;

@end

@implementation MyOrderTopView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.allButton = [self setupButtonWithTitle:@"所有"];
    self.allButton.selected = YES;
    [self addSubview:self.allButton];
    self.moneyButton = [self setupButtonWithTitle:@"待付款"];
    [self addSubview:self.moneyButton];
    self.useButton = [self setupButtonWithTitle:@"待使用"];
    [self addSubview:self.useButton];
    self.commitButton = [self setupButtonWithTitle:@"待评论"];
    [self addSubview:self.commitButton];
    
    self.allLineView = [self setupLineView];
    self.allLineView.hidden = NO;
    [self addSubview:self.allLineView];
    self.moneyLineView = [self setupLineView];
    [self addSubview:self.moneyLineView];
    self.useLineView = [self setupLineView];
    [self addSubview:self.useLineView];
    self.commitLineView = [self setupLineView];
    [self addSubview:self.commitLineView];
}


- (void)layoutSubviews{
    
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self).offset(25 * SCREENH6);
    }];
    
    [self.allLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.allButton);
        make.height.mas_equalTo(3 * SCREENH6);
        make.top.mas_equalTo(self.allButton.mas_bottom).offset(2 * SCREENH6);
    }];
    
    [self.moneyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.allButton);
        make.leading.mas_equalTo(self.allButton.mas_trailing).offset(20 * SCREENH6);
    }];
    
    [self.moneyLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.moneyButton);
        make.height.centerY.mas_equalTo(self.allLineView);
    }];
    
    [self.useButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.moneyButton);
        make.leading.mas_equalTo(self.moneyButton.mas_trailing).offset(20 * SCREENH6);
    }];
    
    [self.useLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.useButton);
        make.height.centerY.mas_equalTo(self.moneyLineView);
    }];
    
    [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.useButton);
        make.leading.mas_equalTo(self.useButton.mas_trailing).offset(20 * SCREENH6);
    }];
    
    [self.commitLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.commitButton);
        make.height.centerY.mas_equalTo(self.useLineView);
    }];
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xFFE055);
    lineView.hidden = YES;
    
    return lineView;
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    button.selected = NO;
    
    return button;
}


- (void)clickChangeButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.allButton) {
        
        type = @"10";
        if (!button.selected) {
            
            self.allButton.selected = YES;
            self.allLineView.hidden = NO;
            self.moneyButton.selected = NO;
            self.moneyLineView.hidden = YES;
            self.useButton.selected = NO;
            self.useLineView.hidden = YES;
            self.commitButton.selected = NO;
            self.commitLineView.hidden = YES;
        }
    }else if (button == self.moneyButton){
        
        type = @"0";
        if (!button.selected) {
            
            self.allButton.selected = NO;
            self.allLineView.hidden = YES;
            self.moneyButton.selected = YES;
            self.moneyLineView.hidden = NO;
            self.useButton.selected = NO;
            self.useLineView.hidden = YES;
            self.commitButton.selected = NO;
            self.commitLineView.hidden = YES;
        }
    }else if (button == self.useButton){
        
        type = @"1";
        if (!button.selected) {
            
            self.allButton.selected = NO;
            self.allLineView.hidden = YES;
            self.moneyButton.selected = NO;
            self.moneyLineView.hidden = YES;
            self.useButton.selected = YES;
            self.useLineView.hidden = NO;
            self.commitButton.selected = NO;
            self.commitLineView.hidden = YES;
        }
    }else if (button == self.commitButton){
        
        type = @"3";
        if (!button.selected) {
            
            self.allButton.selected = NO;
            self.allLineView.hidden = YES;
            self.moneyButton.selected = NO;
            self.moneyLineView.hidden = YES;
            self.useButton.selected = NO;
            self.useLineView.hidden = YES;
            self.commitButton.selected = YES;
            self.commitLineView.hidden = NO;
        }
    }
    
    if (self.myOrderTopViewDelegate && [self.myOrderTopViewDelegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.myOrderTopViewDelegate clickItemButton:type];
    }
}

@end
